/**
 * 知识赛相关的接口替换成了新的接口，参数、返回结构均有变化，在这里统一处理，页面就可以保证最小改动了
 */

import axios from '@/libs/axios1.js'
let jsonHeaders = { 'content-type': 'application/json;charset=UTF-8' }

/*
 * 护网提交和查询接口
 * */
export const submitFile = (data) => {
  return axios({
    url: `/referee/designer/report`,
    method: 'post',
    data,
    headers: jsonHeaders
  })
}
export const updateFile = (data, id) => {
  return axios({
    url: `/referee/designer/report/${id}`,
    method: 'PUT',
    data,
    headers: jsonHeaders
  })
}

// export const getFlagTask = (params) => {
//   return axios({
//     url: `/particular/experimenter/tasks`,
//     method: 'get',
//     params,
//     headers: jsonHeaders
//   });
// };

export const uploadDefenseFiles = (data) => {
  return axios({
    url: `/compete/userSubmitRecord/AWDPlus/defense`,
    method: 'post',
    data,
    headers: jsonHeaders
  })
}
export const isAllowUploadDefenseFile = (data) => {
  return axios({
    url: `/compete/userSubmitRecord/AWDPlus/defense/submitRule`,
    method: 'post',
    headers: jsonHeaders,
    data
  })
}

//用户和场景关联接口(知识赛)
export const usertoSencen = (obj) => {
  let params = {
    sceneId: obj.refId,
    sceneType: obj.sceneType,
    teamUsers: []
  }
  let users = obj.users || []
  let teamIds = []
  let ret = {}
  users.forEach((item) => {
    if (!teamIds.includes(item.teamId)) {
      teamIds.push(item.teamId)
      ret[item.teamId] = {
        teamId: item.teamId,
        teamName: item.teamName,
        users: [{ userId: item.id, userName: item.userName }]
      }
    } else {
      ret[item.teamId].users.push({
        userId: item.id,
        userName: item.userName
      })
    }
  })
  Object.values(ret).forEach((item) => {
    params.teamUsers.push(item)
  })
  return axios({
    url: `/compete/sceneUser`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}
//修改用户场景关联接口(知识赛)
export const usertoSencenModify = ({ refId, dto }) => {
  let params = {
    sceneId: refId,
    sceneType: dto.sceneType,
    teamUsers: []
  }
  let users = dto.users || []
  let teamIds = []
  let ret = {}
  users.forEach((item) => {
    if (!teamIds.includes(item.teamId)) {
      teamIds.push(item.teamId)
      ret[item.teamId] = {
        teamId: item.teamId,
        teamName: item.teamName,
        users: [{ userId: item.id, userName: item.userName }]
      }
    } else {
      ret[item.teamId].users.push({
        userId: item.id,
        userName: item.userName
      })
    }
  })
  Object.values(ret).forEach((item) => {
    params.teamUsers.push(item)
  })
  return axios({
    url: `/compete/sceneUser/${refId}`,
    method: 'put',
    data: params,
    headers: jsonHeaders
  })
}
//用户任务关系列表(知识赛)
export const usertasklist = (sceneId) => {
  return new Promise((resolve) => {
    axios({
      url: `/compete/sceneUser/scene/${sceneId}`,
      method: 'get',
      headers: jsonHeaders
    }).then((res) => {
      let { code, data, message } = res
      let { groupList, list } = data
      list.forEach((item) => (item.refGroupId = item.teamId))
      resolve({
        code: code,
        message: message,
        attribute: {
          groupList: groupList,
          list: list
        }
      })
    })
  })
}

//获取工程下所有的虚拟机
export const getAllVm = (params) => {
  if (!params.ipAddress) {
    params.ipAddress = params.ip
  }
  return axios({
    //  url: `topo-edit/deployed/project/servers`,
    url: `topo-edit/deployed/vms`,
    method: 'get',
    params: params,
    headers: jsonHeaders
  })
}

//flag下发
export const flagVms = (params) => {
  return axios({
    url: `/flag-center/config/projects/${params.projectId}/secret`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}
//flag下发虚拟机查询
export const getFlagVmsStatus = (params) => {
  return axios({
    url: `/flag-center/config/secret/status`,
    method: 'get',
    params: params,
    headers: jsonHeaders
  })
}

//下发流量端口ids
export const setPorts = (params) => {
  return axios({
    url: `/data/flow/filters/add`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}

//删除下发流量接口
export const delflowfilter = (params) => {
  return axios({
    url: `/data/flow/filters/remove`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}
//查询下发流量接口
export const queryflowfilter = (params) => {
  return axios({
    url: `/data/flow/filters/query`,
    method: 'get',
    params: params,
    headers: jsonHeaders
  })
}

// 查询流量记录
export const queryflowList = (params) => {
  return axios({
    url: `/data/flow/search`,
    method: 'get',
    params: params,
    headers: jsonHeaders
  })
}

//保存为模板
export const saveastemplate = (id, obj) => {
  return axios({
    url: `/dcscenetmpl/drillTmpl/create/${id}`,
    params: obj,
    method: 'post',
    headers: jsonHeaders
  })
}
//获取模板列表
export const gettemplatelist = (obj) => {
  return axios({
    url: `/dcscenetmpl/drillTmpl/pages`,
    params: obj,
    method: 'get',
    headers: jsonHeaders
  })
}

//根据工程id查询状态
export const getstatusbyprjid = (params) => {
  return axios({
    url: `/topo-edit/project/${params}`,
    method: 'get',
    headers: jsonHeaders
  }).then(data => { // 格式化因为返回变了，原来接口返回的attribute 现在返回 data
    data.attribute = data.data
    return data
  });
}

//创建
export const createTarget = (params) => {
  return axios({
    // url:`/nsrep-targetinfo/targetManage/createOrupdateTarget`,
    url: `/resource-manager/target`,
    method: 'POST',
    headers: jsonHeaders,
    data: params
  })
}

//修改靶标
export const updateTarget = (id, params) => {
  return axios({
    url: `/resource-manager/target/${id}`,
    method: 'PUT',
    headers: jsonHeaders,
    data: params
  })
}

//删除靶标
export const delTarget = (id) => {
  return axios({
    // url:`/nsrep-targetinfo/targetManage/deleteTarget?targetId=${id}`,
    url: `/resource-manager/target/${id}`,
    method: 'DELETE',
    headers: jsonHeaders
  })
}

//查询靶标列表
export const queryTargetList = (targetDto, params) => {
  return new Promise((resolve) => {
    axios({
      // url:`/nsrep-targetinfo/targetManage/queryTargetPage`,
      url: `/resource-manager/target/list`,
      method: 'POST',
      params: params,
      data: targetDto,
      headers: jsonHeaders
    }).then((data) => {
      if (data && data.attribute === undefined) {
        data.attribute = {
          data: data.data && data.data.items
        }
      }
      resolve(data)
    })
  })
}

//核心路由器下发查询
export const getHxRouterVmsStatus = (params) => {
  return axios({
    url: `/remote/executions`,
    method: 'get',
    params: params,
    headers: jsonHeaders
  })
}

//核心路由器下发批量查询
export const getHxRouterVmsStatusByIds = (params) => {
  return axios({
    url: `/remote/executions/ids`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}

//删除核心路由器下发
export const delHxRouterVms = (projectId, params) => {
  return axios({
    url: `/remote/executions/projects/${projectId}/delete`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}

//核心路由器下发
export const hxRouterVms = (projectId, params) => {
  return axios({
    url: `/remote/executions/projects/${projectId}`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}

//获取用户的详细信息,包含登录信息
export const getUserInfo = (userId) => {
  return axios({
    url: `/identity/company/users/${userId}`,
    method: 'get',
    headers: jsonHeaders
  })
}

/*********任务页面接口**********/
export const saveoutbandTask = (params) => {
  return axios({
    url: `/process/tasks`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}

export const getTaskList = (params) => {
  let copy = Object.assign({}, params) // 拷贝一份，不然有影响到页面数据
  if (copy.pageIndex === undefined) {
    copy.pageIndex = copy.page
  }
  if (copy.pageSize === undefined) {
    copy.pageSize = copy.rows
  }
  return axios({
    url: `/process/tasks`,
    method: 'get',
    params: copy,
    headers: jsonHeaders
  })
}

export const deleteTask = (taskId) => {
  return axios({
    url: `/process/tasks/${taskId}`,
    method: 'delete',
    headers: jsonHeaders
  })
}

export const taskDetail = (taskId) => {
  return axios({
    url: `/process/tasks/${taskId}`,
    method: 'get',
    headers: jsonHeaders
  })
}

export const amendTask = (workingAreaTaskId, amendTaskData) => {
  return axios({
    url: `/process/tasks/${workingAreaTaskId}`,
    method: 'put',
    data: amendTaskData,
    headers: jsonHeaders
  })
}

export const activeTask = (taskId, active) => {
  return axios({
    url: `/process/tasks/${taskId}/execute?operation=${active}`,
    method: 'post',
    headers: jsonHeaders
  })
}

export const batchActiveTask = (params) => {
  return axios({
    url: `/process/tasks/batch/execute`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}

export const taskDetailHis = (taskId) => {
  return axios({
    url: `/process/tasks/${taskId}/histories`,
    method: 'get',
    headers: jsonHeaders
  })
}

export const deleteManyTask = (taskIds) => {
  return axios({
    url: `/process/tasks/${taskIds}`,
    method: 'delete',
    headers: jsonHeaders
  })
}

//一键执行场景内的任务
export const hangUpTask = (params) => {
  return axios({
    url: `/process/tasks/hangUps?scenarioId=${params.sceneId}&operation=${params.op}&taskMode=${params.taskMode}`,
    method: 'post',
    data: {},
    headers: jsonHeaders
  })
}

export const hostDetail = (deviceId) => {
  return axios({
    url: `/topo-edit/deployed/vms/${deviceId}`,
    method: 'get',
    headers: jsonHeaders
  })
}

export const getTaskListDetailData = (params) => {
  return axios({
    url: `/process/overview/task/countByStatus`,
    method: 'get',
    params: params,
    headers: jsonHeaders
  })
}

export const showConfiguration = (value) => {
  return axios({
    url: `/tasktemplate/item/${value}`,
    method: 'get',
    headers: jsonHeaders
  })
}

//查询设备列表
export const getDeviceList = (projectId, params) => {
  return axios({
    url: `/topo-edit/vm/list/${projectId}`,
    method: 'get',
    params: params,
    headers: jsonHeaders
  })
}

export const loadConfiguration = (params) => {
  return axios({
    url: `checker/scripts`,
    method: 'get',
    params: params,
    headers: jsonHeaders
  })
}

//获取配置列表
export const loadItemConfiguration = (params) => {
  return axios({
    url: `tasktemplate/item`,
    type: 'get',
    params: params
  })
}

//获取服务列表
export const getServerList = (params) => {
  return axios({
    url: `/netns/config/servers`,
    type: 'get',
    params: params
  })
}

//获取flag服务器
export const getFlagServerList = (params) => {
  return axios({
    url: `/topo-edit/project/flag-servers`,
    type: 'get',
    params: params
  })
}

//获取单次提交flag任务启动详情
export const getFlagOnceDetail = (taskId) => {
  return axios({
    url: `/process/tasks/${taskId}/users`,
    type: 'get'
  })
}

//会话查询
export const getSessions = (params) => {
  let copy = Object.assign({}, params) // 拷贝一份，不然有影响到页面数据
  if (copy.pageIndex === undefined) {
    copy.pageIndex = copy.page
  }
  if (copy.pageSize === undefined) {
    copy.pageSize = copy.rows
  }
  return axios({
    url: `/access/sessions`,
    params: copy,
    type: 'get'
  })
}

export const getSessionsBydevice = (params) => {
  let copy = Object.assign({}, params) // 拷贝一份，不然有影响到页面数据
  if (copy.pageIndex === undefined) {
    copy.pageIndex = copy.page
  }
  if (copy.pageSize === undefined) {
    copy.pageSize = copy.rows
  }

  return axios({
    url: `/access/sessions/deviceIdList?pageIndex=${copy.pageIndex}&pageSize=${copy.pageSize}`,
    method: 'post',
    data: params,
    // params:copy,
    headers: jsonHeaders
  })
}

//会话查询
export const pushNettingData = (params) => {
  return axios({
    url: `/notice/internal/pushNettingData/${params.projectId}`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}

//会话记录
export const getSessionsRecord = (sessionId) => {
  return axios({
    url: `/access/sessions/${sessionId}/recording`,
    type: 'get',
    timeout: 0
  })
}

//删除录屏
export const delSession = (id) => {
  return axios({
    url: `/access/storage/deleteRecording?sessionId=${id}`,
    method: 'get',
    headers: jsonHeaders
  })
}

//录屏转码
export const sessionTranscoding = (projectId) => {
  return axios({
    url: `/access/storage/transcoding?projectId=${projectId}`,
    method: 'post',
    data: {},
    headers: jsonHeaders
  })
}

//录屏转码状态
export const getTranscodingStatus = (projectId) => {
  return axios({
    url: `/access/storage/transcodingStatus?projectId=${projectId}`,
    method: 'get',
    data: {},
    headers: jsonHeaders
  })
}

/**
 * 下发路由规则，包含竞赛系统路由规则、团队路由规则、基础规则、端口转发规则
 * @param {*} sceneId
 * @returns
 */
export const issuedRules = (sceneId) => {
  return axios({
    url: `/compete/rule/${sceneId}`,
    method: 'post',
    headers: jsonHeaders
  })
}

/**
 * 查询部署详情
 * @param {*} param0
 * @returns
 */
export const getDeployedInfo = ({ pageIndex, pageSize, projectId, vmType, id }) => {
  return axios({
    url: `/topo-edit/device_scripts/devices/deployed`,
    method: 'get',
    params: {
      pageIndex,
      pageSize,
      projectId,
      vmType,
      id
    },
    headers: jsonHeaders
  })
}

/**
 * 查询路由规则下发记录
 * @param {*} param0
 * @returns
 */
export const getIssuedRulesRecord = ({ pageIndex, pageSize, projectId, deviceId }) => {
  return axios({
    url: `/topo-edit/device_scripts/logs`,
    method: 'get',
    params: {
      pageIndex,
      pageSize,
      projectId,
      deviceId
    },
    headers: jsonHeaders
  })
}

/**
 * 查询下发规则的执行脚本信息
 * @param {*} scriptId
 * @returns
 */
export const getScriptInfo = (scriptId) => {
  return axios({
    url: `/topo-edit/device_scripts/${scriptId}`,
    method: 'get',
    headers: jsonHeaders
  })
}

/**
 * 删除指定的脚本执行记录
 * @param {*} scriptId
 * @returns
 */
export const deleteScript = (scriptId) => {
  return axios({
    url: `/topo-edit/device_scripts/${scriptId}`,
    method: 'delete',
    headers: jsonHeaders
  })
}

export const getDefenseFiles = (params) => {
  return axios({
    url: `/compete/userSubmitRecord/AWDPlus/defense/page`,
    method: 'get',
    headers: jsonHeaders,
    params
  })
}

/**
 * 重新下发脚本
 * @param {*} data
 * @returns
 */
export const executeScriptAgain = (data) => {
  return new Promise((resolve) => {
    return axios({
      url: `/topo-edit/device_scripts/batch`,
      method: 'post',
      headers: jsonHeaders,
      data
    }).then((res) => {
      resolve(res)
    })
  })
}

// 应急演练，演练报告和记录管理
export const getCompeteSummarize = (params) => {
  return axios({
    url: `/compete/summarize/page`,
    method: 'get',
    headers: jsonHeaders,
    params
  })
}

export const saveSummarize = (data) => {
  return axios({
    url: `/compete/summarize`,
    method: 'post',
    headers: jsonHeaders,
    data
  })
}

/**
 *
 * @param id stepid
 * @param data
 * @returns {*}
 */
export const putSummarize = (id, data) => {
  return axios({
    url: `/compete/summarize/${id}`,
    method: 'put',
    headers: jsonHeaders,
    data
  })
}

export const downloadSummarize = (sceneId, type, ids = [], fn) => {
  return axios({
    url: `/compete/summarize/downloadZip/${sceneId}/${type}`,
    method: 'post',
    data: ids,
    headers: {
      'Content-Type': 'application/json; application/octet-stream'
    },
    responseType: 'blob',
    onDownloadProgress: fn,
    timeout: 0
  })
}

export const downloadDrillReport = (sceneId, fn) => {
  return axios({
    url: `/compete/summarize/exportWord/${sceneId}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json; application/octet-stream'
    },
    responseType: 'blob',
    onDownloadProgress: fn,
    timeout: 0
  })
}

export const delSummarize = (id) => {
  return axios({
    url: `/compete/summarize/${id}`,
    method: 'delete',
    headers: jsonHeaders
  })
}

export const saveCurSelectStep = (data) => {
  return axios({
    url: `/compete/summarize/step`,
    method: 'post',
    headers: jsonHeaders,
    data
  })
}

// 查询当前选择的步骤
export const querySelectSteps = (params) => {
  return axios({
    url: `/compete/summarize/step`,
    method: 'get',
    headers: jsonHeaders,
    params
  })
}

// 应急演练框选保存(添加，修改)
export const saveProcessFrame = (data) => {
  return axios({
    url: `/compete/frame`,
    method: 'post',
    headers: jsonHeaders,
    data
  })
}

// 删除
export const delProcessFrame = (id) => {
  return axios({
    url: `/compete/frame/${id}`,
    method: 'delete',
    headers: jsonHeaders
  })
}

// 查询
export const getProcessFrame = (params) => {
  return axios({
    url: `/compete/frame`,
    method: 'get',
    params,
    headers: jsonHeaders
  })
}

//单个加入节点
export const addFrameNode = (data) => {
  return axios({
    url: `/compete/frame/addOneRelation`,
    method: 'post',
    headers: jsonHeaders,
    data
  })
}

//单个移除节点
export const removeFrameNode = (id) => {
  return axios({
    url: `/compete/frame/delOneRelation/${id}`,
    method: 'delete',
    headers: jsonHeaders
  })
}

//GET /accessGroupVPN/onlineVPNCount
// VPN在线数量统计
export const getOnlineVPNCount = (params) => {
  return axios({
    url: '/compete/accessGroupVPN/onlineVPNCount',
    method: 'get',
    headers: jsonHeaders,
    params
  })
}

// 团队vpn列表
export const teamGroupList = (params) => {
  return axios({
    url: '/compete/accessGroupVPN/teamVPN',
    method: 'get',
    headers: jsonHeaders,
    params
  })
}

// 个人vpn列表
export const teamPersonList = (params) => {
  return axios({
    url: '/compete/accessGroupVPN/userVPN',
    method: 'get',
    headers: jsonHeaders,
    params
  })
}

// vpn状态查询列表
export const getVpnStatus = (params) => {
  return axios({
    url: '/compete/accessGroupVPN/getVpnStatus',
    method: 'get',
    headers: jsonHeaders,
    params
  })
}

// 批量分发
export const redisDistribute = (params) => {
  return axios({
    url: '/compete/accessGroupVPN/redisDistribute',
    method: 'post',
    headers: jsonHeaders,
    data: params
  })
}

// vpn批量下载
export const downLoadFilevpn = (params) => {
  return new Promise((resolve) => {
    axios({
      url: `/compete/accessGroupVPN/downLoadFile`,
      method: 'get',
      headers: jsonHeaders,
      params,
      responseType: 'blob'
    }).then((res) => {
      if (res.type == 'application/json') {
        const reader = new FileReader()
        reader.readAsText(res, 'utf-8')
        reader.onload = function() {
          resolve(JSON.parse(reader.result))
        }
      } else {
        resolve(res)
      }
    })
  })
}

// 获取scriptId
export const getScriptId = (params) => {
  return axios({
    url: `/compete/accessGroupVPN/getScriptId`,
    method: 'get',
    headers: jsonHeaders,
    params
  })
}

// 重新生成VPN
export const vpnRegeneration = (projectId, testId, params) => {
  return axios({
    url: `/compete/accessGroupVPN/vpnRegeneration?projectId=${projectId}&testId=${testId}`,
    method: 'post',
    headers: jsonHeaders,
    data: params
  })
}

// 初始生成
export const VpnGenerate = (projectId, testId) => {
  return axios({
    url: `/compete/accessGroupVPN/vpnGenerate?projectId=${projectId}&testId=${testId}`,
    method: 'post',
    headers: jsonHeaders
  })
}

// 对实网测试、仿真对抗、众测演练-按月统计
export const monthStatistic = (params) => {
  return axios({
    url: `/compete/ns/monthStatistic`,
    method: 'get',
    params,
    headers: jsonHeaders
  })
}

// 上报分靶场录屏回放数据
export const reportScreenPlayback = (params, sceneId) => {
  return axios({
    url: `/compete/contest/${sceneId}/reportScreenPlayback`,
    method: 'post',
    data: params,
    headers: jsonHeaders
  })
}

// 查询录屏回放分页列表
export const getScreenPlaybackPage = (params) => {
  return axios({
    url: `/compete/contest/${params.sceneId}/screenPlaybackPage`,
    method: 'get',
    params,
    headers: jsonHeaders
  })
}
